package com.dmooo.yxmmerchant.view.presenter;

import android.content.Context;

import com.common.mvpbase.BasePresenter;
import com.common.net.listener.HttpOnNextListener;
import com.common.util.LogUtil;
import com.dmooo.yxmmerchant.domain.BankListBean;
import com.dmooo.yxmmerchant.domain.MyBankBean;
import com.dmooo.yxmmerchant.view.contract.BankContract;
import com.dmooo.yxmmerchant.view.model.BankModel;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class BankPrestener extends BasePresenter<BankContract.BankView> implements BankContract.BankPtr {
    private BankModel model;
    GsonBuilder builder = new GsonBuilder();

    public BankPrestener(BankContract.BankView mView, Context context) {
        super(mView);
        model = new BankModel(context);
        builder.serializeNulls();
    }

    @Override
    public void addBank(Map<String, Object> map) {
        model.addBank(map, new HttpOnNextListener() {
            @Override
            public void onNext(Object o) {
                mView.addBankSuccess();
            }

            @Override
            public void onFail(Object o) {
                mView.errorMsg(o.toString());
            }

            @Override
            public void onStart() {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void deleteBank(String token, String id, final int index) {
        model.deleteBank(token, id, index, new HttpOnNextListener() {
            @Override
            public void onNext(Object o) {
                mView.delBankSuccess(index);
            }

            @Override
            public void onFail(Object o) {
                mView.errorMsg(o.toString());
            }

            @Override
            public void onStart() {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void getBankType() {
        model.getBankType(new HttpOnNextListener() {
            @Override
            public void onNext(Object o) {
                LogUtil.d("sdafsd", o.toString());
                try {
                    JSONArray array = new JSONObject(builder.create().toJson(o)).getJSONArray("list");
                    List<BankListBean.BankItem> addressBeans = new ArrayList<>();
                    for (int i = 0; i < array.length(); i++) {
                        addressBeans.add(new Gson().fromJson(array.getJSONObject(i).toString(), BankListBean.BankItem.class));
                    }
                    mView.showBankTypeList(addressBeans);
                } catch (JSONException e) {
                    e.printStackTrace();
                    mView.errorMsg("数据解析错误");
                }
            }

            @Override
            public void onFail(Object o) {
                mView.errorMsg(o.toString());
            }

            @Override
            public void onStart() {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void getMyBankList(String token) {
        model.getMyBankList(token, new HttpOnNextListener() {
            @Override
            public void onNext(Object o) {
                LogUtil.d("dsfa", o.toString());
                try {
                    JSONArray array = new JSONObject(builder.create().toJson(o)).getJSONArray("list");
                    List<MyBankBean.BankItem> addressBeans = new ArrayList<>();
                    for (int i = 0; i < array.length(); i++) {
                        addressBeans.add(new Gson().fromJson(array.getJSONObject(i).toString(), MyBankBean.BankItem.class));
                    }
                    mView.showMyBankList(addressBeans);
                } catch (JSONException e) {
                    e.printStackTrace();
                    mView.errorMsg("数据解析错误");
                }
            }

            @Override
            public void onFail(Object o) {
                mView.errorMsg(o.toString());
            }

            @Override
            public void onStart() {

            }

            @Override
            public void onComplete() {

            }
        });
    }
}
